package com.diankangwo.factory.controller;

import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.PageUtils;
import com.diankangwo.common.utils.Query;
import com.diankangwo.common.utils.R;
import com.diankangwo.common.validator.ValidatorUtils;
import com.diankangwo.common.validator.group.AddGroup;
import com.diankangwo.common.validator.group.UpdateGroup;
import com.diankangwo.factory.annotation.LoginUser2;
import com.diankangwo.factory.dto.TdAddressDTO;
import com.diankangwo.factory.entity.TdAddress;
import com.diankangwo.factory.entity.TdUserinfo;
import com.diankangwo.factory.service.*;
import com.diankangwo.sys.entity.SysUserEntity;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员Controller
 * Created by wuye on 2017/9/15.
 */
@RestController
@RequestMapping("/fac/user")
public class TdUserInfoController {

    @Autowired
    private TdUserInfoService tdUserInfoService;

    @Autowired
    private TdAddressService tdAddressService;

    @Autowired
    private TdProvinceService tdProvinceService;

    @Autowired
    private TdCityService tdCityService;

    @Autowired
    private TdDistrictService tdDistrictService;


    /**
     * 获取会员列表
     */
    @ApiOperation(value = "获取会员列表", notes = "")
    @ApiImplicitParam(name = "params", value = "查询条件", required = true, dataType = "Map<String,Object>")
    @RequestMapping("/list")
    @RequiresPermissions("fac:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TdUserinfo> userList = tdUserInfoService.queryList(query);
        int total = tdUserInfoService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }


    /**
     * 根据ID获取会员信息
     */
    @ApiOperation(value = "根据ID获取会员信息", notes = "")
    @ApiImplicitParam(name = "userinfoId", value = "会员id", required = true, dataType = "Integer")
    @SysLog("根据ID获取会员信息")
    @RequestMapping("/queryById")
    @RequiresPermissions("fac:user:queryById")
    public R queryById(@LoginUser2 SysUserEntity sysUserEntity, int userinfoId) {
        System.out.println(sysUserEntity.getTdFactoryid());
        TdUserinfo userinfo = tdUserInfoService.queryById(userinfoId);
        return R.ok().put("tdUserInfo", userinfo);
    }

    /**
     * 添加会员
     */
    @ApiOperation(value = "添加会员", notes = "")
    @ApiImplicitParam(name = "tdUserinfo", value = "会员", required = true, dataType = "TdUserinfo")
    @SysLog("保存会员")
    @RequestMapping("/save")
    @RequiresPermissions("fac:user:save")
    public R save(@RequestBody TdUserinfo tdUserinfo) {
        ValidatorUtils.validateEntity(tdUserinfo, AddGroup.class);
        tdUserInfoService.save(tdUserinfo);
        return R.ok();
    }

    /**
     * 修改会员
     */
    @ApiOperation(value = "修改会员", notes = "")
    @ApiImplicitParam(name = "tdUserinfo", value = "修改会员", required = true, dataType = "TdUserinfo")
    @SysLog("修改会员")
    @RequestMapping("/update")
    @RequiresPermissions("fac:user:update")
    public R update(@RequestBody TdUserinfo tdUserinfo) {
        ValidatorUtils.validateEntity(tdUserinfo, UpdateGroup.class);

        tdUserInfoService.update(tdUserinfo);

        return R.ok();
    }

    /**
     * 删除会员
     */
    @ApiOperation(value = "删除会员", notes = "")
    @ApiImplicitParam(name = "userinfoId", value = "会员ID", required = true, dataType = "Integer")
    @SysLog("删除会员")
    @RequestMapping("/delete")
    @RequiresPermissions("fac:user:delete")
    public R delete(int userinfoId) {
        tdUserInfoService.delete(userinfoId);
        return R.ok();
    }

    /**
     * 获取指定用户的收获地址列表
     */
    @ApiOperation(value = "获取指定用户的收获地址列表", notes = "")
    @ApiImplicitParam(name = "id", value = "会员ID", required = true, dataType = "Integer")
    @SysLog("获取指定用户的收获地址列表")
    @RequestMapping("/findAddress")
    @RequiresPermissions("fac:user:findAddress")
    public R findAddress(int id) {
        TdUserinfo tdUserinfo = tdUserInfoService.queryById(id);
        List<TdAddressDTO> tdAddressDTOList = new ArrayList<>();
        List<TdAddress> tdAddressList = tdAddressService.selectByUid(tdUserinfo.getUid());
        for (int i = 0; i < tdAddressList.size(); i++) {
            TdAddressDTO tdAddressDTO = new TdAddressDTO();
            tdAddressDTO.setDetailedaddress(tdAddressList.get(i).getDetailedaddress());
            tdAddressDTO.setIsdefault(tdAddressList.get(i).getIsdefault());
            tdAddressDTO.setPhone(tdAddressList.get(i).getPhone());
            tdAddressDTO.setPostcode(tdAddressList.get(i).getPostcode());
            tdAddressDTO.setUid(tdAddressList.get(i).getUid());
            tdAddressDTO.setUsername(tdAddressList.get(i).getUsername());
            tdAddressDTO.setProvinceName(tdProvinceService.queryById(tdAddressList.get(i).getProvinceid()).getProvincename());
            tdAddressDTO.setCityName(tdCityService.selectByPrimaryKey(tdAddressList.get(i).getCityid()).getCityname());
            tdAddressDTO.setProvinceName(tdDistrictService.selectByPrimaryKey(tdAddressList.get(i).getDistrictid()).getDistrictname());
            tdAddressDTOList.add(tdAddressDTO);
        }
        return R.ok().put("tdAddress", tdAddressDTOList);
    }
}
